﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace ShoppingList
{
    public partial class EditItemPage : PhoneApplicationPage
    {
        private ItemViewModel backup;

        public EditItemPage()
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }
            InitializeComponent();
            if (App.ViewModel.CurrentList.CurrentItem == null)
            {
                App.ViewModel.CurrentList.CurrentItem = new ShoppingList.ItemViewModel() { Amount = 1 };
                PageTitle.Text = "Add item";
                //this.ItemName.Focus();
            }
            else
            {
                backup = new ShoppingList.ItemViewModel()
                {
                    Amount = App.ViewModel.CurrentList.CurrentItem.Amount,
                    Comment = App.ViewModel.CurrentList.CurrentItem.Comment,
                    ItemName = App.ViewModel.CurrentList.CurrentItem.ItemName,
                    Price = App.ViewModel.CurrentList.CurrentItem.Price
                };
            }

            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;
        }

        private static void HandleInvalidChars(object sender, KeyEventArgs e)
        {
            var tb = (sender as TextBox);
            if (e.PlatformKeyCode == 188 || e.Key == Key.Decimal || e.PlatformKeyCode == 190)
            {
                tb.Text = tb.Text.Replace(".", "") + ".";
                tb.Select(tb.Text.Length, 0);
                e.Handled = true;
            }
            else
            {
                e.Handled = !(e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9);
            }
        }

        private void btn_Add_Click(object sender, RoutedEventArgs e)
        {
            App.ViewModel.CurrentList.CurrentItem.Amount = decimal.Round(App.ViewModel.CurrentList.CurrentItem.Amount, 2);
            App.ViewModel.CurrentList.CurrentItem.Price = decimal.Round(App.ViewModel.CurrentList.CurrentItem.Price, 2);
            App.ViewModel.CurrentList.CurrentItem.NotifyPropertyChanged("Selected");
            if (!App.ViewModel.CurrentList.ItemsOnList.Contains(App.ViewModel.CurrentList.CurrentItem) &&
                !App.ViewModel.CurrentList.ItemsInCart.Contains(App.ViewModel.CurrentList.CurrentItem))
            {
                App.ViewModel.CurrentList.ItemsOnList.Add(App.ViewModel.CurrentList.CurrentItem);
            }
            App.ViewModel.CurrentList.OnPropertyChanged("CartSum");
            App.ViewModel.CurrentList.OnPropertyChanged("ListSum");
            NavigationService.GoBack();
        }

        private void ItemName_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                //btn_AddItem.Focus();
                txb_Amount.Focus();
                txb_Amount.SelectAll();
                e.Handled = true;
            }
        }

        private void txb_Amount_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                txb_Price.Focus();
                txb_Price.SelectAll();
                e.Handled = true;
            }
            else HandleInvalidChars(sender, e);
        }

        private void txb_Price_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                txb_Comment.Focus();
                txb_Comment.SelectAll();
                e.Handled = true;
            }
            else HandleInvalidChars(sender, e);
        }

        private void txb_Comment_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                btn_AddItem.Focus();
                e.Handled = true;
            }
        }

        private void AddItem_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (backup != null)
            {
                App.ViewModel.CurrentList.CurrentItem.Amount = backup.Amount;
                App.ViewModel.CurrentList.CurrentItem.Comment = backup.Comment;
                App.ViewModel.CurrentList.CurrentItem.ItemName = backup.ItemName;
                App.ViewModel.CurrentList.CurrentItem.Price = backup.Price;
            }
        }

        private void AddItem_Loaded(object sender, RoutedEventArgs e)
        {
            txb_ItemName.Focus();
            if (txb_ItemName.Text.Length > 0)
                txb_ItemName.SelectAll();
        }

        private void txb_ItemName_LostFocus(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(txb_ItemName.Text))
            {
                txb_ItemName.Text = String.Format(ShoppingList.Resources.NewItemName,
                    App.ViewModel.CurrentList.ItemsInCart.Count + App.ViewModel.CurrentList.ItemsOnList.Count + 1);
            }
        }
    }
}